package com.domain.utils;

import java.util.Random;
import java.util.UUID;

public class UUIDUtils {

    public static final String VERIFY_CODE = "0123456789";

    public static final String INVITATION_CODE = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";

    public static String getRandomUUID() {
        String uuid = UUID.randomUUID().toString();
        uuid = uuid.replace("-", "");
        return uuid;
    }

    //生成一个0-n的随机数
    public static Integer Random(Integer n) {
        Double r = Math.random();
        Integer index = ArithmeticUtil.mul(r.toString(), n.toString()).intValue();
        return index;
    }


    /**
     * 使用指定源生成码
     *
     * @param verifySize 长度
     * @param sources    字符源
     * @return
     */
    public static String generateVerifyCode(int verifySize, String sources) {
        if (sources == null || sources.length() == 0) {
            sources = VERIFY_CODE;
        }
        int codesLen = sources.length();
        Random rand = new Random(System.currentTimeMillis());
        StringBuilder verifyCode = new StringBuilder(verifySize);
        for (int i = 0; i < verifySize; i++) {
            verifyCode.append(sources.charAt(rand.nextInt(codesLen - 1)));
        }
        return verifyCode.toString();
    }

    /**
     * 获取区间随机数
     *
     * @param iStart
     * @param iEnd
     * @return
     */
    public static double getRandom(double iStart, double iEnd) {
        double start = 0;
        double end = 0;
        if (iStart > iEnd) {
            start = iEnd;
            end = iStart;
        } else {
            start = iStart;
            end = iEnd;
        }
        return (double) (start + Math.random() * ((end - start )));
    }

    public static String VerifyCode(int verifySize) {
        return generateVerifyCode(verifySize, VERIFY_CODE);
    }

    public static String InvitationCode(int verifySize) {
        return generateVerifyCode(verifySize, INVITATION_CODE);
    }

}
